package com.dmatek.uwb.xml.util.iparse.bean;

import java.util.List;
import java.util.Map;

import org.dom4j.Element;

import com.dmatek.uwb.util.tool.StringUtil;
import com.dmatek.uwb.xml.dom.bean.DomElement;
import com.dmatek.uwb.xml.util.iassign.IAssignAttributeTool;
import com.dmatek.uwb.xml.util.iparse.IDomParse;
/***
 * 解析xml节点元素并生成对象的抽象类
 * @author zhangfu
 * @data 2019年6月14日 下午2:12:15
 * @Description
 */
public abstract class BaseDomParse<T extends DomElement> implements IDomParse<T> {
	private IAssignAttributeTool iAssignTool;
	public BaseDomParse(IAssignAttributeTool iAssignTool) {
		this.iAssignTool = iAssignTool;
	}
	@SuppressWarnings("unchecked")
	@Override
	public T parse(Element element, Map<String, Object> map) throws Exception {
		List<Element> properties = element.elements("property");
		T domElement = createEmpty(element.attributeValue("class"));
		if(null != domElement) {
			for (Element property : properties) {
				// 利用反射设置相关类并生成对象
				String val = property.attributeValue("value");
				String ref = property.attributeValue("ref");
				getiAssignTool().assign(property.attributeValue("name"), StringUtil.isEmpty(ref)?val:ref , property ,domElement, map);
			}
		}
		return domElement;
	}
	protected abstract T createEmpty(String clazzName);
	public IAssignAttributeTool getiAssignTool() {
		return iAssignTool;
	}
	public void setiAssignTool(IAssignAttributeTool iAssignTool) {
		this.iAssignTool = iAssignTool;
	}
}
